﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BonsaiScrum.Bus
{
    public class TransactionHandler<TCommand, TCommandHandler>
        where TCommand : class, ICommand
        where TCommandHandler : ICommandHandler<TCommand>
    {
        private readonly ITransactional _unitOfWork;

        public TransactionHandler(ITransactional unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public void Execute(TCommand command, TCommandHandler commandHandler)
        {
            try
            {
                _unitOfWork.BeginTransaction();
                commandHandler.Handle(command);
                _unitOfWork.Commit();
            }
            catch
            {
                _unitOfWork.Rollback();
                throw;
            }
        }
    }
}
